//
//  PlatformLoginView.swift
//  MyWeiBlog
//
//  Created by laichunhui on 15/10/9.
//  Copyright © 2015年 lch. All rights reserved.
//

import UIKit

/// DoubleTextViewDelegate协议
@objc protocol PlatformLoginViewDelegate: NSObjectProtocol
{
    func platformLoginViewForSinaLogin(platformLoginView: PlatformLoginView)
    func platformLoginViewForWeChatLogin(platformLoginView: PlatformLoginView)
    
    optional func incrementForCount(count: Int) -> Int
}

class PlatformLoginView: UIView
{
    private let sinaLoginBtn = UIButton()
    private let weChatLoginBtn = UIButton()
    private let loginProtocolView = UIImageView()
    
    weak var delegate: PlatformLoginViewDelegate?
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        //设置背景
        self.backgroundColor = UIColor.blackColor()
        
        loginProtocolView.image = UIImage(named:"sm_login_logo")
        loginProtocolView.contentMode = UIViewContentMode.ScaleAspectFit
        self.addSubview(loginProtocolView)
        
        //设置三方登录按钮
        setupLoginBtn()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupLoginBtn()
    {   //新浪
        sinaLoginBtn.setImage(UIImage(named: "sm_login_sina_btns"), forState: .Normal)
        sinaLoginBtn.setBackgroundImage(UIImage(named: "sm_login_sina_btn"), forState: .Normal)
        sinaLoginBtn.addTarget(self, action:"clickLoginButton:", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(sinaLoginBtn)
        //微信
        weChatLoginBtn.setImage(UIImage(named: "sm_login_wx_btns"), forState: .Normal)
        weChatLoginBtn.setBackgroundImage(UIImage(named: "sm_login_wx_btn"), forState: .Normal)
        weChatLoginBtn.addTarget(self, action:"clickLoginButton:", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(weChatLoginBtn)
    }
    
    func clickLoginButton(button:UIButton)
    {
        button == sinaLoginBtn ? delegate?.platformLoginViewForSinaLogin(self) : delegate?.platformLoginViewForWeChatLogin(self)
    }
    
    override func layoutSubviews()
    {
        let margin:CGFloat = 20.0
        let btnWH:CGFloat = height
        
        sinaLoginBtn.frame = CGRectMake(margin, 0, btnWH, btnWH)
        
        loginProtocolView.frame = CGRectMake(margin + btnWH, 0, width - (btnWH + margin) * 2, height)
        
        weChatLoginBtn.frame = CGRectMake(width - margin - btnWH, 0, btnWH, btnWH)
        
    }
}
